<template>
    <div class="cmt-container">
        <h3>发表评论</h3>
        <hr>
        <textarea v-model="cmtText" placeholder="请输入要BB的内容（最多BB120个字）" maxlength="120"></textarea>

        <mt-button @click="addComment" type="primary" size="large">发表评论</mt-button>

        <div class="cmt-list">
            <div class="cmt-item" v-for="(item, i) in comment">
                <div class="cmt-title">
                    第{{ i+1 }}楼&nbsp;&nbsp;用户：{{ item.user_name }}&nbsp;&nbsp;
                    发表时间：{{ item.add_time | dateFormat }}
                </div>
                <div class="cmt-body">
                    {{ item.content }}
                </div>
            </div>
        </div>

        <mt-button type="danger" size="large" plain @click="getMoreComments">加载更多</mt-button>

    </div>
</template>

<script>
    import { Toast } from 'mint-ui'

    export default {
        name: "",
        props: ['id'],
        data(){
            return {
                comment: [],
                pageIndex: 1,
                cmtText: '' //评论输入内容
            }
        },
        created(){
            this.getComments()
        },
        methods: {
            getComments(){ //获取所有评论
                this.$http.get('static/data/comment.json').then(res => {
                    if(res.body.status === 0){
                        console.log(this.id);

                        var ret = res.body.message.find(item => item.id === parseInt(this.id))
                        if(!ret){
                            Toast('获取评论失败...')
                            return
                        }

                        var cmt = ret.contents[this.pageIndex]

                        if(cmt === undefined){
                            console.log('已经没有更多的评论了...')
                            Toast('已经没有更多的评论了...')
                            return
                        }

                        // 每当获取新评论数据时，不要把老数据覆盖
                        this.comment = this.comment.concat(cmt)
                    }else{
                        Toast('获取评论失败...')
                    }
                })
            },
            getMoreComments(){ //获取更对评论
                this.pageIndex++
                this.getComments()
            },
            addComment(){ //发表评论
                if(!this.cmtText.trim().length){
                    Toast('内容不能为空,请重新输入')
                    return
                }
                this.comment.unshift({
                    user_name: '匿名用户',
                    add_time: Date.now(),
                    content: this.cmtText.trim()
                })
                this.cmtText = ''
                Toast('添加成功')
            }
        }
    }
</script>

<style lang="less" scoped>
.cmt-container{
    h3{
        font-size: 18px;
    }
    textarea{
        font-size: 14px;
        padding: 8px;
        margin: 0;
        height: 99px;
    }
    .cmt-list{
        margin: 2px 0;
        .cmt-item{
            font-size: 13px;
            .cmt-title{
                line-height: 35px;
                background-color: #ccc;
            }
            .cmt-body{
                line-height: 35px;
                text-indent: 2em;
            }
        }
    }
}
</style>